<?php
/**
 * XmuSlh, web platform of Student Association Federation of Xiamen University
 * 
 * Rewritting one or some pages of StatusNet. 
 * To get the original version, please visit <http://status.net/>.
 * 
 * StatusNet, the distributed open-source microblogging tool
 * 
 * LICENCE: This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * Resetpassword action.
 * 
 * 重置登陆用账户的密码
 * 
 * @author		ytukie <ytukie@gmail.com>
 * @version		0.1
 */

// XmuSlh only.
if ( !defined('XMUSLH')) { exit(1); }

// Requirements.
require_once INSTALLDIR . '/lib/slh_util.php';
require_once INSTALLDIR . '/widgets/slh_form_resetpassword.php';

require_once INSTALLDIR . '/classes/User.php';


class ResetpasswordAction extends Action
{
	/**
	 * Has some instructions?
	 */
	var $instruction = null;
	
	/**
	 * Login username, password, newpassword and newconfirm.
	 */
	var $username = null;
	var $password = null;
	var $newpassword = null;
	var $newconfirm = null;
	var $authcode = null;
	
	function title()
	{
		return '重置密码';
	}
	
	function prepare($args)
	{
		parent::prepare($args);
		
		return true;
	}
	
	function handle($args)
	{
		parent::handle($args);
		
		if ($_SERVER['REQUEST_METHOD'] == 'POST') {
			// CSRF protection.
			$this->checkSessionToken();
			
			try {
				$this->username = $this->getTrimmedArg('username');
				$this->password = $this->getArg('password');
				$this->newpassword = $this->getArg('newpassword');
				$this->newconfirm = $this->getArg('newconfirm');
				$this->authcode = $this->getArg('authcode');
				
				if (strcmp($this->authcode, $_SESSION['authcode']) != 0) {
					$this->showAction('验证码出错.');
				} else if (empty($this->username)) {
					$this->showAction('请输入用户名.');
				} else if (empty($this->password) || empty($this->newpassword) || empty($this->newconfirm)) {
					$this->showAction('请输入密码.');
				} else if (strlen($this->newpassword) < 6 ||
						   strlen($this->newconfirm) < 6) {
					$this->showAction('密码长度必须长于或等于6位.');
				}else if (0 != strcmp($this->newpassword, $this->newconfirm)) {
					$this->showAction('两次输入的新密码不一致.');
				} else {
					$authenticatedUser = User::authenticateUser($this->username, md5($this->password));
					
					if (false == $authenticatedUser) {
						$this->showAction('请输入原来正确的用户名和密码.');
					} else {
						if(User::resetPassword($this->username, md5($this->newpassword))) {
							// Logout all.
							slh_set_user(null);
							slh_real_login(false);
			
							$this->showAction('修改密码成功.');
						} else {
							$this->showAction('修改密码失败!');
						}
					}
				}
			} catch (Exception $e) {
				$this->clientError($e->getMessage());
			}
		} else {
			$this->showAction();
		}
	}
	

	/**
	 * Overrideing the showInnerContent().
	 */
	function showInnerContent()
	{
		$this->element('h3', null, '重置密码!');
		$this->element('hr');
		
		// Show the instruction.
		$this->showInstruction();
		
		$form = new ResetpasswordForm($this, $this->args);
		$form->show();
		
		$this->element('hr');
	}
	
// Overrided ends //////////////////////////////////////////////////////////////

	/**
	 * Shows the intruction this page.
	 */
	function showInstruction()
	{
		if ($this->instruction) {
			$this->elementStart('div', array('class' => 'cls_div_instruction'));
			$this->instruction($this->instruction);
			$this->elementEnd('div');
		}
	}
	
	/**
	 * Shows the form with message or instruction.
	 */
	function showAction($instruction = null)
	{
		$this->instruction = $instruction;
		$this->showPage();
	}
}

?>